package com.hucsmn.stumansys.servlet.student;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.hucsmn.stumansys.entity.Admin;
import com.hucsmn.stumansys.entity.Student;
import com.hucsmn.stumansys.service.AdminService;
import com.hucsmn.stumansys.service.StudentService;
import com.hucsmn.stumansys.utils.ConvertUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 学生信息添加 API。
 *
 * 接收 HTTP 参数
 *      card    卡号。1-24 位数字。
 *      name    姓名。1-8 个字符。
 *      gender  性别。false 男，true 女。
 *      birth   出生日期。可选，非空时格式必须为 yyyy-MM-dd。
 *      email   邮箱。可选，非空时格式必须为邮箱格式。
 *      remark  备注。可选。
 *
 * 响应 JSON 数据
 *      code    状态码。0 表示成功，1 表示失败，2 表示权限错误。
 *      msg     操作出错原因。
 *
 * 备注
 *      要求 session.login 必须是 Admin 且非空。
 *
 * @author hucsmn
 */
@WebServlet("/api/student/add")
public class StudentAddServlet extends HttpServlet {
    private final StudentService studentService = StudentService.getInstance();
    private final Gson gson = new GsonBuilder().create();

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=UTF-8");

        String card = ConvertUtils.normalizeText(request.getParameter("card"));
        String name = ConvertUtils.normalizeText(request.getParameter("name"));
        boolean gender = ConvertUtils.parseFlag(request.getParameter("gender"))
                .orElseGet(() -> "女".equals(request.getParameter("gender")));
        String birth = ConvertUtils.normalizeText(request.getParameter("birth"));
        String email = ConvertUtils.normalizeText(request.getParameter("email"));
        String remark = ConvertUtils.normalizeText(request.getParameter("remark"));

        Student student = new Student();
        student.setCard(card);
        student.setName(name);
        student.setGender(gender);
        student.setBirth(birth);
        student.setEmail(email);
        student.setRemark(remark);

        JsonObject result = new JsonObject();
        HttpSession session = request.getSession();
        if (!(session.getAttribute("login") instanceof Admin)) {
            result.addProperty("code", 2);
            result.addProperty("msg", "没有权限");
        } else if (!studentService.add(student)) {
            result.addProperty("code", 1);
            if (ConvertUtils.validateStudent(student)) {
                result.addProperty("msg", "学号已存在");
            } else {
                result.addProperty("msg", "学生信息格式不正确");
            }
        } else {
            result.addProperty("code", 0);
            result.addProperty("msg", "");
        }

        PrintWriter out = response.getWriter();
        out.write(gson.toJson(result));
        out.flush();
        out.close();
    }
}
